package com.goddess.yeecolor.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.goddess.yeecolor.Modle.ActivityFragmentModle;
import com.goddess.yeecolor.Modle.controlInfo;
import com.goddess.yeecolor.R;
import com.goddess.yeecolor.activity.ExerciseItem2Activit;
import com.goddess.yeecolor.activity.ExerciseItemActivity;
import com.goddess.yeecolor.activity.Exerciseltem3Activity;
import com.goddess.yeecolor.activity.ReusingRaymentActivity;
import com.goddess.yeecolor.interfaceAll.OnClickListener;
import com.goddess.yeecolor.tools.ImageUtils;
import com.goddess.yeecolor.tools.JumpIntents;
import com.goddess.yeecolor.tools.LogUtils;
import com.goddess.yeecolor.tools.MyUrl;
import com.goddess.yeecolor.tools.OkttpUtils;
import com.goddess.yeecolor.tools.SharePreceUtils;
import com.goddess.yeecolor.view.dialog_progress;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *我的活动适配器
 */
public class MyHuoDongAdapter extends BaseRecyclerAdapter<MyHuoDongAdapter.viewHodler> {
    private List<ActivityFragmentModle> modleList;
    private Context context;
    private Activity activity;
    public MyHuoDongAdapter(List<ActivityFragmentModle> modleList, Context context, Activity activity){
        this.modleList=modleList;
        this.context=context;
        this.activity=activity;
    }

//    public enum TYPE{
//        TYPE_ONE,TYPE_TWO,TYPE_THREE
//    }

    @Override
    public viewHodler getViewHolder(View view) {
        return new viewHodler(view);
    }

    @Override
    public viewHodler onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v= LayoutInflater.from(context).inflate(R.layout.item_myhudong_one,parent,false);
        return new viewHodler(v);
    }

    private List<controlInfo>list;
    private String tag;
    private  Bundle bundle;
    @Override
    public void onBindViewHolder(viewHodler holder, int position, boolean isItem) {
        ActivityFragmentModle modle=modleList.get(position);
        ImageUtils.getInstance().ImageStart(context,modle.getThumb(),holder.image_rl);
        holder.image_rl.setScaleType(ImageView.ScaleType.FIT_XY);

        holder.item_myhuodong_title.setText(modle.getTitle());
        holder.item_myhuodong_time.setText(modle.getAdd_date());
        holder.item_myhuodong_pices.setText(modle.getPrice());
        list=modle.getControlInfoList();
        for (int i=0;i<list.size();i++){
            controlInfo info=list.get(i);
            String state= info.getName();
            if (state.equals("delete")){
                holder.item_myhuodong_cancal.setText("删除订单");
            }
            else if(state.equals("complete")){
                holder.item_myhuodong_pay.setText("已完成");
            }
            else if(state.equals("views")){
                holder.item_myhuodong_pay.setText("查看活动");
            }
            else if(state.equals("cancel")){
                holder.item_myhuodong_cancal.setText("取消活动");
            }
            else if(state.equals("pay")){
                holder.item_myhuodong_pay.setText("前往支付");
            }
        }
        holder.item_myhuodong_pay.setOnClickListener((view)->{
            tag=holder.item_myhuodong_pay.getText().toString().trim();
            int posi=holder.getLayoutPosition();
            if (tag.equals("已完成")){

            }
            else if(tag.equals("前往支付")){
                bundle=new Bundle();
                bundle.putString("buy_id",modle.getBuy_id());
                bundle.putString("type",modle.getTyped());
                bundle.putString("order_id",modle.getOrder_id());
                JumpIntents.startIntent(activity,ReusingRaymentActivity.class,bundle);//跳转到支付界面
            }
            else if (tag.equals("查看活动")){
                int state=modle.getStatae();
                bundle=new Bundle();
                bundle.putString("typed",modle.getCollection_id()+"");
                bundle.putString("title","");

                bundle.putString("id",modle.getEventsid()+"");
                if (state==2){
                    JumpIntents.startIntent(activity, ExerciseItem2Activit.class,bundle);
                }
                else if (state==1){
                    JumpIntents.startIntent(activity, Exerciseltem3Activity.class,bundle);
                }
                else if (state==0){
                    JumpIntents.startIntent(activity, ExerciseItemActivity.class,bundle);
                }
            }
        });
        /**
         * 第二个
         */
        holder.item_myhuodong_cancal.setOnClickListener((view)->{
            tag=holder.item_myhuodong_cancal.getText().toString().trim();
            int posi=holder.getLayoutPosition();
            LogUtils.getInstence().info("");
            if (tag.equals("删除订单")){
                setProgress();
                map=new HashMap<Object, Object>();
                map.put("unique", SharePreceUtils.getOther("user","unique"));
                map.put("records_id",modle.getId()+"");
                LogUtils.getInstence().info(MyUrl.DETELEBUY+"&unique="+SharePreceUtils.getOther("user","unique")+"&order_id="+modle.getId());
                OkttpUtils.getIntence().OkttpPost(MyUrl.DETELEBUY,map,handler,1);
            }
            else if(tag.equals("取消活动")){
                setProgress();
                map=new HashMap<Object, Object>();
                map.put("unique", SharePreceUtils.getOther("user","unique"));
                map.put("records_id",modle.getId()+"");
                OkttpUtils.getIntence().OkttpPost(MyUrl.DETELEBUY,map,handler,1);
            }
            modleList.remove(posi);
            notifyItemRemoved(posi);
            notifyItemRangeChanged(0,modleList.size()-2);
        });

    }

    private Map<Object,Object>map;
    @Override
    public int getAdapterItemCount() {
        return modleList.size();
    }

    class viewHodler extends RecyclerView.ViewHolder{
        private LinearLayout linerlyou_pay,linerlyou_finish,linerlyou_look;
        private TextView item_myhuodong_delete,item_myhuodong_look,item_myhuodong_delete2,item_myhuodong_finish,item_myhuodong_cancal,item_myhuodong_pay;
        private ImageView image_rl;
        private TextView item_myhuodong_title,item_myhuodong_time,item_myhuodong_pices;
        public viewHodler(View itemView) {
            super(itemView);
            image_rl= (ImageView) itemView.findViewById(R.id.item_myhuodong_imageview);
            item_myhuodong_title= (TextView) itemView.findViewById(R.id.item_myhuodong_title);
            item_myhuodong_time= (TextView) itemView.findViewById(R.id.item_myhuodong_time);
            item_myhuodong_pices= (TextView) itemView.findViewById(R.id.item_myhuodong_pices);
            item_myhuodong_delete= (TextView) itemView.findViewById(R.id.item_myhuodong_delete);
            item_myhuodong_delete2= (TextView) itemView.findViewById(R.id.item_myhuodong_delete2);
            item_myhuodong_finish= (TextView) itemView.findViewById(R.id.item_myhuodong_finish);
            item_myhuodong_look= (TextView) itemView.findViewById(R.id.item_myhuodong_look);
            item_myhuodong_cancal= (TextView) itemView.findViewById(R.id.item_myhuodong_cancal);
            item_myhuodong_pay= (TextView) itemView.findViewById(R.id.item_myhuodong_pay);
            linerlyou_look= (LinearLayout) itemView.findViewById(R.id.linerlyou_look);
            linerlyou_finish= (LinearLayout) itemView.findViewById(R.id.linerlyou_finish);
            linerlyou_pay= (LinearLayout) itemView.findViewById(R.id.linerlyou_pay);

            /**
             * 每一个item的点击事件
             */
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.OnClick(view,getLayoutPosition());
                }
            });

        }
    }

    /**
     * 点击每一行的接口
     */
    private OnClickListener onItemClickListener;
    public interface  OnItemClicklistener{
        void OnItemClick(View view,int position);
    }

    /**
     * 单行点击事件
     * @param onItemClickListener
     */
    public void setOnItemClickListener(OnClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }

    /**
     * 清空适配器数据
     */
    public void clear(){
        clear(modleList);
    }

    /**
     * 添加数据
     */
    public void insert(ActivityFragmentModle modle,int position){
        insert(modleList,modle,position);
    }
    private String jsonString;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try {
                jsonString=msg.getData().getString("result");

//                JSONObject object=new JSONObject(jsonString);
                if (dialog_progress!=null&&dialog_progress.isShowing()){
                    dialog_progress.dismiss();
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    };

    private com.goddess.yeecolor.view.dialog_progress dialog_progress;
    private void setProgress(){
        dialog_progress=new dialog_progress(context);
        dialog_progress.requestWindowFeature(Window.FEATURE_NO_TITLE);

        dialog_progress.setCanceledOnTouchOutside(false);//按返回键可以关闭进度条
        dialog_progress.show();
    }
}
